package org.wattdepot.cli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.cli.processor.DailySensorDataCommand;

/**
 * Test for the DailySensorData command.
 * 
 * @author Kendyll Doi
 * @author Kimberly Heu
 *
 */
public class TestDailySensorDataCommand extends CommandLineInterface {
  
  /**
   * Tests the DailySensorData methods. 
   * @throws Exception If problems occur. 
   */
  @Test
  public void testDailySensorDataCommand() throws Exception {
    
    List<String> commandList = new ArrayList<String>(); // List of commands
    
    commandList.add("dailysensordata");
    commandList.add("SIM_WAIAU_7");
    commandList.add("day");
    commandList.add("2009-10-31");
    
    DailySensorDataCommand command = new DailySensorDataCommand(); // command
    
    // The result from executing the individual method
    String methodResult = 
      WattSensorDataDay.wattSensorDataDay("SIM_WAIAU_7", "2009-10-31", client);
    // The result from executing corresponding command
    String commandResult = command.doCommand(commandList);
    
    // Ensure answer contains the endpoint times
    assertTrue("Checking method result", methodResult.contains("2009-10-31T00:00:00"));
    assertTrue("Checking method result", methodResult.contains("2009-10-31T23:45:00"));
    // Ensure both results are the same
    assertEquals("Checking method and command results are equal", methodResult, commandResult);
  }
  
  /**
   * Test that the input to the command is validated.
   * 
   * @throws Exception if problem occurs
   */
  @Test
  public void testInputValidation() throws Exception {
    
    List<String> commandList = new ArrayList<String>();
    commandList.add("dailysensordata");
    commandList.add("nosource");
    commandList.add("day");
    commandList.add("2009-10-31");
    
    DailySensorDataCommand command = new DailySensorDataCommand();
    String commandResult = command.doCommand(commandList);
    
    // Test for an incorrect source

    assertTrue("Checking invalid source", commandResult.contains("No data found"));
    
    
    commandList.remove(1);
    commandList.add(1, "SIM_WAIAU_7");
    commandList.remove(2);
    commandList.add(2, "daay");
    
    // Test for invalid use of "timestamp" argument
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid format for command", commandResult.contains("Incorrect syntax"));
    
    // Test for too few arguments
    commandList.remove(3);
    commandResult = command.doCommand(commandList);
    assertTrue("Checking invalid number of commands", commandResult.contains("Incorrect number"));
  }
}
